package com.fierce.cloud.ota.controller;

import com.fierce.cloud.core.web.ResultGenerator;
import com.fierce.cloud.core.web.responseDto.Response;
import com.fierce.cloud.ota.dao.AppInfoMapper;
import com.fierce.cloud.ota.dao.VersionInfoMapper;

import com.fierce.cloud.ota.model.responseVo.CountStatisticVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author: Fierce
 * @Date: 2021/4/17 11:08
 */
@RestController
@RequestMapping("/statistic")
public class StatisticController {

    @Autowired
    AppInfoMapper appInfoMapper;
    @Autowired
    VersionInfoMapper versionInfoMapper;

    @GetMapping("/appCount")
    public Response appCountStatistic() {
        Date endDate = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        Date startDate = calendar.getTime();
        List<Date> dateList = findDates(startDate, endDate);
        List<CountStatisticVo> result = new ArrayList<>();
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("MM-dd");
        dateList.forEach(date -> {
            int count = appInfoMapper.countAppInfoByDate(simpleDateFormat1.format(date));
            result.add(new CountStatisticVo(simpleDateFormat2.format(date),count));
        });
        return ResultGenerator.genSuccessResult(result);
    }

    @GetMapping("/versionCount")
    public Response versionCountStatistic() {
        Date endDate = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        Date startDate = calendar.getTime();
        List<Date> dateList = findDates(startDate, endDate);
        List<CountStatisticVo> result = new ArrayList<>();
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("MM-dd");
        dateList.forEach(date -> {
            int count = versionInfoMapper.countVersionCountByDate(simpleDateFormat1.format(date));
            result.add(new CountStatisticVo(simpleDateFormat2.format(date),count));
        });
        return ResultGenerator.genSuccessResult(result);
    }

    public static List<Date> findDates(Date dStart, Date dEnd) {
        Calendar cStart = Calendar.getInstance();
        cStart.setTime(dStart);

        List dateList = new ArrayList();
        //别忘了，把起始日期加上
        dateList.add(dStart);
        // 此日期是否在指定日期之后
        while (dEnd.after(cStart.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            cStart.add(Calendar.DAY_OF_MONTH, 1);
            dateList.add(cStart.getTime());
        }
        return dateList;
    }

}
